package com.example.friend.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.friend.R;
import com.example.friend.mvp.ui.util.SoftInputUtils;
import com.example.okhttputils.mvp.base.BaseActivity;
import com.example.okhttputils.mvp.base.BasePresenter;
import com.h.cheng.pselector.base.load.PSConfig;
import com.h.cheng.pselector.base.load.PSManager;
import com.h.cheng.pselector.bean.BaseMedia;
import com.h.cheng.pselector.view.SpaceDecoration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: admins
 * date:   2019/6/26 19:15
 * desc:
 */
public class SendTextActivity extends BaseActivity {
    private TextView tvTitle,tvCancel,tvSend;
    private RecyclerView rvSend;
    private ImageView imageView;
    private List<String> pathList = new ArrayList<>();
    private List<File> fileList = new ArrayList<>();
    private ArrayList<BaseMedia> imgList = new ArrayList<>();
    private PhotoAdapter photoAdapter;
    private int currentMaxCount = 9;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_send_text;
    }

    @Override
    protected void initView() {
        tvCancel = findViewById(R.id.tv_cancel);
        tvSend= findViewById(R.id.tv_send);
        tvTitle = findViewById(R.id.tv_title);
        rvSend = findViewById(R.id.rv_photo);
        imageView = findViewById(R.id.iv_next);
        tvCancel.setText("取消");
        tvSend.setText("发送");
        tvTitle.setText("发表文字");
        initAdapter();
    }

    @Override
    protected void addListener() {
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        photoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //隐藏软键盘
                SoftInputUtils.hideSoftInput(SendTextActivity.this);
                if (adapter.getData().get(position) == null) {
//                    //最后一个
                    PSConfig config = new PSConfig()
                            .withSpanCount(3)
                            .withMaxCount(currentMaxCount)
                            .setUseSysAlbum(false);
                    PSManager.of(config).forDialog(SendTextActivity.this, rvSend);
                } else {
//                    // 启动预览原图界面
                    PSManager.of().forPreView(context, getMedias(), position);
                }
            }
        });

        photoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.iv_photo_delete){
                    adapter.getData().remove(position);
                    if (!adapter.getData().contains(null)) {
                        adapter.getData().add(null);
                    }
                    currentMaxCount++;
                    adapter.notifyDataSetChanged();
                }
            }
        });
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SendTextActivity.this,RecordViewActivity.class));
            }
        });
    }



    private void initAdapter() {
        imgList.add(null);
        photoAdapter = new PhotoAdapter(imgList);
        rvSend.setLayoutManager(new GridLayoutManager(context, 3));
        rvSend.addItemDecoration(new SpaceDecoration(10));
        rvSend.setAdapter(photoAdapter);
    }

    private void initListener() {

    }
    private List<String> getPath() {
        pathList.clear();
        ArrayList<BaseMedia> list = (ArrayList<BaseMedia>) imgList.clone();
        if (list.contains(null)) {
            list.remove(null);
        }
        for (BaseMedia media : list) {
            pathList.add(media.getPath());
        }
        return pathList;

    }

    private ArrayList<BaseMedia> getMedias() {

        ArrayList<BaseMedia> list = (ArrayList<BaseMedia>) imgList.clone();
        if (list.contains(null)) {
            list.remove(null);
        }

        return list;

    }

    public class PhotoAdapter extends BaseQuickAdapter<BaseMedia, BaseViewHolder> {
        public PhotoAdapter(@Nullable List data) {
            super(R.layout.item_circle_photo, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BaseMedia item) {

            if (helper.getLayoutPosition() == getData().size() - 1) {
                if (item == null) {
                    //最后一个
                    helper.setImageResource(
                            R.id.iv_photo, R.mipmap.push_evaluate);
                    helper.getView(R.id.iv_photo_delete).setVisibility(View.GONE);
                } else {
                    Glide.with(context).load(item.getPath()).apply(new RequestOptions().centerCrop()).into((ImageView) helper.getView(R.id.iv_photo));
                    helper.getView(R.id.iv_photo_delete).setVisibility(View.VISIBLE);
                }
            } else {
                Glide.with(context).load(item.getPath()).apply(new RequestOptions().centerCrop()).into((ImageView) helper.getView(R.id.iv_photo));
                helper.getView(R.id.iv_photo_delete).setVisibility(View.VISIBLE);
            }

            helper.addOnClickListener(R.id.iv_photo_delete);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PSManager.getPermissionResult(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        PSManager.getResult(requestCode, resultCode, data, this, new PSManager.ResultCallBack() {
            @Override
            public void formCamera(String path) {
                BaseMedia baseMedia = new BaseMedia(path, path, "0");
                if (imgList.contains(null)) {
                    imgList.remove(null);
                    imgList.add(baseMedia);
                    currentMaxCount--;
                }
                if (imgList.size() < 3) {
                    imgList.add(null);
                }
                photoAdapter.notifyDataSetChanged();
            }

            @Override
            public void formSysAlbum(String path) {

            }

            @Override
            public void formMultAlbm(ArrayList<BaseMedia> list) {
                if (imgList.contains(null)) {
                    imgList.remove(null);
                    imgList.addAll(list);
                }
                currentMaxCount = currentMaxCount - list.size();
                if (imgList.size() < 9) {
                    imgList.add(null);
                }

                photoAdapter.notifyDataSetChanged();
            }
        });
    }
}
